package main;

public class SalaryWorker {

	private float monthlyBaseSalary;
	private float monthlyBonus = 0;
	private float monthlyWorkRatio = 1;
	private float monthlyNetIncome;
	private float monthlyPersonalWelfare; /*
										 * monthlyPersonalWelfare includes
										 * social_security_funds and housingfund
										 */
	private float monthlyPersonalTax;
	private float monthly_social_security_funds;
	private float monthlyHousingfund;
	private String location;
	private float personalSS;
	private float personalHousingfund;

/**
 * 
 * @param monthlyBaseSalary
 * @param monthlyBonus
 * @param monthlyWorkRatio
 * @param location
 * @param welfareRatioTableModel
 * @param taxRatioTableModel
 */
	public SalaryWorker(float monthlyBaseSalary, float monthlyBonus,float monthlyWorkRatio,String location,WelfareRatioTableModel welfareRatioTableModel,
			TaxRatioTableModel taxRatioTableModel) {
		this.monthlyBaseSalary = monthlyBaseSalary;
		this.monthlyBonus=monthlyBonus;
		this.monthlyWorkRatio=monthlyWorkRatio;
		this.location=location;
		
		float monthlyActualSalary = (this.monthlyBaseSalary + this.monthlyBonus)
				* this.monthlyWorkRatio;
		monthlyPersonalWelfare = calculate_monthlyPersonalWelfare(
				monthlyActualSalary, welfareRatioTableModel);
		float salaryForTax = monthlyActualSalary - monthlyPersonalWelfare;
		monthlyPersonalTax = calculate_monthlyPersonalTax(salaryForTax, taxRatioTableModel);
		monthlyNetIncome = calculate_monthlyNetIncome(monthlyActualSalary,
				monthlyPersonalWelfare, monthlyPersonalTax);
		monthly_social_security_funds = calculate_monthly_social_security_fund(
				monthlyActualSalary, welfareRatioTableModel);
		monthlyHousingfund = calculate_monthlyHousingfund(monthlyActualSalary,
				welfareRatioTableModel);

	}

/**
 * 
 * @param monthlyActualSalary
 * @param welfareRatioTableModel
 * @return
 */
	private float calculate_monthlyHousingfund(float monthlyActualSalary,
			WelfareRatioTableModel welfareRatioTableModel) {
		float personalHousingFundRatio=welfareRatioTableModel.get_personal_housingfund_ratio(location);
		float companyHousingFundRatio=welfareRatioTableModel.get_company_housingfund_ratio(location);
		float salaryForHousingfund=monthlyActualSalary>welfareRatioTableModel.get_toplimit_housingfund(location)?welfareRatioTableModel.get_toplimit_housingfund(location):monthlyActualSalary;
		float totalHousingfund=salaryForHousingfund*(personalHousingFundRatio+companyHousingFundRatio);
		personalHousingfund=salaryForHousingfund*personalHousingFundRatio;
		
		return totalHousingfund;
	}

/**
 * 
 * @param monthlyActualSalary
 * @param welfareRatioTableModel
 * @return
 */
	private float calculate_monthly_social_security_fund(
			float monthlyActualSalary, WelfareRatioTableModel welfareRatioTableModel) {
		float personelSSR=welfareRatioTableModel.get_personal_social_security_ratio(location);
		float companySSR=welfareRatioTableModel.get_company_social_security_ratio(location);
		float salaryForSocialSecurity=monthlyActualSalary>welfareRatioTableModel.get_toplimit_social_security(location)?welfareRatioTableModel.get_toplimit_social_security(location):monthlyActualSalary;
		float totalSSF=salaryForSocialSecurity*(personelSSR+companySSR);
		personalSS=salaryForSocialSecurity*personelSSR;
		if (location.equals(Constant.welfare_suzhou_downtown)) {
			totalSSF+=5;
			personalSS+=5;
		}
		
		return totalSSF;
	}

/**
 * 
 * @param monthlyActualSalary
 * @param monthlyPersonalWelfare
 * @param monthlyPersonalTax
 * @return
 */
	 
	private float calculate_monthlyNetIncome(float monthlyActualSalary,
			float monthlyPersonalWelfare, float monthlyPersonalTax) {
		return monthlyActualSalary - monthlyPersonalWelfare
				- monthlyPersonalTax;
	}

/**
 * 
 * @param monthlyActualSalary
 * @param welfareRatioTableModel
 * @return
 */
	
	public float calculate_monthlyPersonalWelfare(float monthlyActualSalary,
			WelfareRatioTableModel welfareRatioTableModel) {
		float salaryForSocialSecurity=monthlyActualSalary>welfareRatioTableModel.get_toplimit_social_security(location)?welfareRatioTableModel.get_toplimit_social_security(location):monthlyActualSalary;
		float salaryForHousingfund=monthlyActualSalary>welfareRatioTableModel.get_toplimit_housingfund(location)?welfareRatioTableModel.get_toplimit_housingfund(location):monthlyActualSalary;
		float rt_monthlyPersonalWelfare = salaryForSocialSecurity*welfareRatioTableModel.get_personal_social_security_ratio(location)+salaryForHousingfund*welfareRatioTableModel.get_personal_housingfund_ratio(location);
		
		if (location.equals(Constant.welfare_suzhou_downtown)) {
			rt_monthlyPersonalWelfare+=5;
		}

		return rt_monthlyPersonalWelfare;
	}

/**
 * 
 * @param salaryForTax
 * @param taxRatioTableModel
 * @return
 */
	
	public float calculate_monthlyPersonalTax(float salaryForTax,
			TaxRatioTableModel taxRatioTableModel) {
		float levyPoint=taxRatioTableModel.getLevyPoint();
		int levyLevel=taxRatioTableModel.getLevyLevel((salaryForTax-levyPoint)>0?(salaryForTax-levyPoint):salaryForTax);
		float currentTaxRatio=taxRatioTableModel.getCurrentTaxRatio(levyLevel);
		float quickDeduction=taxRatioTableModel.getQuickDeduction(levyLevel);
		float taxLiability=((salaryForTax-levyPoint)>0?(salaryForTax-levyPoint):salaryForTax)*currentTaxRatio-quickDeduction;
		return taxLiability;
	}

	public float getMonthlyNetIncome() {
		return monthlyNetIncome;
	}

	public float getMonthlyPersonalWelfare() {
		return monthlyPersonalWelfare;
	}

	public float getMonthlyPersonalTax() {
		return monthlyPersonalTax;
	}

	public float getMonthly_social_security_funds() {
		return monthly_social_security_funds;
	}

	public float getMonthlyHousingfund() {
		return monthlyHousingfund;
	}

	public float getPersonalSS() {
		return personalSS;
	}

	public float getPersonalHousingfund() {
		return personalHousingfund;
	}

}